/**
 * @overview 创建标注
 * @author dengqiang
 * @description 鲸云CgsLabel
 * @update  2022.04.19
 */
class GLabel {
  constructor() {
    window.GLabelArray = []
  }

  //新增单个标注
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const optionModified = {
      position: option.position,
      name: option.name,
      labelfont: '24px 微软雅黑 ',
      labelfill: option.labelfill || 'rgba(255,0,0,1.0)',
      labeloffset: option.labeloffset || [0, -15],
      labelstroke: option.labelstroke || 'rgba(0, 0, 0, 0.0)', // 描边颜色
      labelstrokewidth: option.labelstroke || 1, //描边宽度，像素单位
      labelscale: option.labelscale || 1, //比例尺
      labelBackgroundColor: option.labelBackgroundColor || 'rgba(1, 1, 1, 0.8)', //背景色
      showLabelBackground: option.showLabelBackground || true, //是否显示背景
      labelBackgroundPadding: option.labelBackgroundPadding || [7, 2], // 背景的内边距，像素单位
    }
    const label = new CgsLabel(optionModified)

    const labelObject = {
      name: name,
      parentName: parentName,
      label: label,
    }
    window.GLabelArray.push(labelObject)
    window.map.addFeature(label)
  }

  //根据name移除单个标注
  removeByName(name) {
    window.GLabelArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeFeature(item.label)
        window.GLabelArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批标注
  removeByPatName(parentName) {
    const length = window.GLabelArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GLabelArray[i].parentName == parentName) {
        window.map.removeFeature(window.GLabelArray[i].label)
        window.GLabelArray.splice(i, 1)
      }
    }
  }

  //移除所有标注
  removeAll() {
    window.GLabelArray.forEach((item) => {
      window.map.removeFeature(item.label)
    })
    window.GLabelArray = []
  }

  //根据name控制单个标注的显隐
  setVisibleByName(name, boolean) {
    window.GLabelArray.forEach((item) => {
      if (item.name == name) {
        item.label.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批标注的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GLabelArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.label.setVisible(boolean)
      }
    })
  }

  //根据name设置单个标注的位置
  setPosition(name, newPosition) {
    window.GLabelArray.forEach((item, index) => {
      if (item.name == name) {
        item.label.setPosition(newPosition)
      }
    })
  }
}

export default GLabel
